package org.ainlolcat.ainscience.utils;

import org.jdom.Element;

/**
 * @author ainlolcat
 *         Date: 5/9/13
 */
public class XMLUtils {


    public static String getSafeString4Attribute(Element element, String attribute, String def){
        try{
            String value = element.getAttributeValue(attribute);
            return value != null ? value : def;
        } catch (Exception e){
            return def;
        }
    }

    public static String getSafeString4Children(Element element, String attribute, String def){
        try{
            return element.getChildTextTrim(attribute);
        } catch (Exception e){
            return def;
        }
    }

    public static double getSafeDouble4Attribute(Element element, String attribute, double def){
        try{
            return Double.parseDouble(element.getAttributeValue(attribute));
        } catch (Exception e){
            return def;
        }
    }

    public static double getSafeDouble4Children(Element element, String attribute, double def){
        try{
            return Double.parseDouble(element.getChildTextTrim(attribute));
        } catch (Exception e){
            return def;
        }
    }

    public static int getSafeInteger4Attribute(Element element, String attribute, int def){
        try{
            return Integer.parseInt(element.getAttributeValue(attribute));
        } catch (Exception e){
            return def;
        }
    }

    public static int getSafeInteger4Children(Element element, String attribute, int def){
        try{
            return Integer.parseInt(element.getChildTextTrim(attribute));
        } catch (Exception e){
            return def;
        }
    }

    public static boolean getSafeBoolean4Attribute(Element element, String attribute, boolean def){
        try{
            return Boolean.parseBoolean(element.getAttributeValue(attribute));
        } catch (Exception e){
            return def;
        }
    }

    public static boolean getSafeBoolean4Children(Element element, String attribute, boolean def){
        try{
            return Boolean.parseBoolean(element.getChildTextTrim(attribute));
        } catch (Exception e){
            return def;
        }
    }
}
